package com.pig4cloud.pig.module.controller.wx;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.wx.UserCoupons;
import com.pig4cloud.pig.module.api.entity.wx.WxUser;
import com.pig4cloud.pig.module.service.wx.IUserCouponsService;
import com.pig4cloud.pig.module.service.wx.IWxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 用户的跃豆购买的抵扣券Controller
 *
 * @author ruoyi
 * @date 2022-04-28
 */
@Validated
@Api(value = "用户的跃豆购买的抵扣券控制器", tags = {"用户的跃豆购买的抵扣券管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/user/coupons")
public class UserCouponsController {

    private final IUserCouponsService userCouponsService;
    private final IWxUserService wxUserService;

    @Value("${ruoyi.imgUrl}")
    private String imgUrl;

    /**
     * 查询用户的跃豆购买的抵扣券列表
     */
    @ApiOperation("查询用户的跃豆购买的抵扣券列表")
    @PreAuthorize("@pms.hasPermission('module:usercoupons:list')")
    @GetMapping("/list")
    public TableDataInfo<UserCoupons> list(@Validated UserCoupons userCoupons) {
        QueryWrapper<UserCoupons> qw = new QueryWrapper<>();
        if (userCoupons.getUserNo()!=null){
            WxUser wxUser1 = wxUserService.selectByUniqueColumn("user_no",userCoupons.getUserNo());
            if (wxUser1!=null){
                qw.eq("user_id",wxUser1.getId());
            }else {
                WxUser wxUser2 = wxUserService.selectByUniqueColumn("id",userCoupons.getUserNo());
                if (wxUser2!=null){
                    qw.eq("user_id",wxUser2.getId());
                }else {
                    qw.eq("user_id",0);
                }
            }
        }
        TableDataInfo<UserCoupons> td = PageUtils.buildDataInfo(userCouponsService.page(PageUtils.buildPage(),qw));
        List<UserCoupons> list = td.getData();
        for (UserCoupons list1:list){
            WxUser wxUser = wxUserService.findById(list1.getUserId());
            if(wxUser!=null){
                list1.setFaceUrl(wxUser.getFaceUrl());
                list1.setNickName(wxUser.getNickName());
                if(StrUtil.isNotEmpty(wxUser.getFaceUrl()) && wxUser.getFaceUrl().indexOf("https") < 0){
                    list1.setFaceUrl(imgUrl + wxUser.getFaceUrl());
                }
            }
        }
        return td;
    }

    /**
     * 导出用户的跃豆购买的抵扣券列表
     */
    @ApiOperation("导出用户的跃豆购买的抵扣券列表")
    @PreAuthorize("@pms.hasPermission('module:usercoupons:export')")
    @SysLog("用户的跃豆购买的抵扣券")
    @GetMapping("/export")
    public List export(@Validated UserCoupons userCoupons) {
        List<UserCoupons> list = userCouponsService.list();
        // ExcelUtil<UserCoupons> util = new // ExcelUtil<UserCoupons>(UserCoupons.class);
        return list;// util.exportExcel(list, "用户的跃豆购买的抵扣券");
    }

    /**
     * 获取用户的跃豆购买的抵扣券详细信息
     */
    @ApiOperation("获取用户的跃豆购买的抵扣券详细信息")
    @PreAuthorize("@pms.hasPermission('module:usercoupons:query')")
    @GetMapping("/{id}")
    public R<UserCoupons> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        UserCoupons userCoupons = userCouponsService.getById(id);
        return R.ok(userCoupons);
    }

    /**
     * 新增用户的跃豆购买的抵扣券
     */
    @ApiOperation("新增用户的跃豆购买的抵扣券")
    @PreAuthorize("@pms.hasPermission('module:usercoupons:add')")
    @SysLog("用户的跃豆购买的抵扣券")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody UserCoupons userCoupons) {
        return R.okByRow(userCouponsService.save(userCoupons) ? 1 : 0);
    }

    /**
     * 修改用户的跃豆购买的抵扣券
     */
    @ApiOperation("修改用户的跃豆购买的抵扣券")
    @PreAuthorize("@pms.hasPermission('module:usercoupons:edit')")
    @SysLog("用户的跃豆购买的抵扣券")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody UserCoupons userCoupons) {
        return R.okByRow(userCouponsService.updateById(userCoupons) ? 1 : 0);
    }

    /**
     * 删除用户的跃豆购买的抵扣券
     */
    @ApiOperation("删除用户的跃豆购买的抵扣券")
    @PreAuthorize("@pms.hasPermission('module:usercoupons:remove')")
    @SysLog("用户的跃豆购买的抵扣券")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(userCouponsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
